package com.gkr.shopping.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gkr.shopping.bean.Shopcar;
import com.gkr.shopping.service.ShopCarService;


@Controller
@RequestMapping("/shopCar")
public class ShopCarController {
	
	@Autowired
	private ShopCarService shopCarService;
	
	@RequestMapping("/addToCar.do")
	public String addShopCar(HttpSession session,@Param("id")int id,@Param("number")int number) {
		
		shopCarService.addArticleToShopCar(session,id,number);
		return "redirect:/shopCar/showShopCar.do";
	}
	
	@RequestMapping("/showShopCar.do")
	public String showShopCar(HttpSession session,Model model) {
		
		List<Shopcar> shopCars = shopCarService.getAllShopCarByUserId(session);
		model.addAttribute("shopCars", shopCars);

		double totalPrice = 0.0;

		for(Shopcar shopcar : shopCars) {
			totalPrice += shopcar.getArticle().getDiscountPrice() * shopcar.getBuynum();
		}
		
		model.addAttribute("totalPrice", totalPrice);

		return "shopCar";
	}
	
		//更新购物车
		@RequestMapping("/updateShopcar.do")
		public String updateShopcar(HttpSession session,@Param("id")int id,@Param("number")int number) {
			shopCarService.updateShopcar(session,id,number);

			return "redirect:/shopCar/showShopCar.do";
		}
		
		//删除购物车
		@RequestMapping("/deleteShopCar.do")
		public String deleteShopCar(HttpSession session,@Param("id")int id) {

			shopCarService.deleteShopcar(session,id);

			return "redirect:/shopCar/showShopCar.do";
		}

	
}
